package com.city.java.base.config;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.city.java.users.pojo.entity.Users;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

/**
 * 用户类型枚举处理器
 */
public class UserTypeHandler extends BaseTypeHandler<Users.UserType> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Users.UserType parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, parameter.getValue());
    }

    @Override
    public Users.UserType getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String value = rs.getString(columnName);
        return value == null ? null : Users.UserType.fromValue(value);
    }

    @Override
    public Users.UserType getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String value = rs.getString(columnIndex);
        return value == null ? null : Users.UserType.fromValue(value);
    }

    @Override
    public Users.UserType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String value = cs.getString(columnIndex);
        return value == null ? null : Users.UserType.fromValue(value);
    }
}